package com.kyrie.utils;

import com.alibaba.druid.util.StringUtils;
import com.kyrie.dto.OperateDictionariesDto;
import com.kyrie.system.mybatis.execption.ServiceException;
import com.kyrie.system.mybatis.service.IDefaultService;
import com.kyrie.vo.TOperationCommTypeVal;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisCluster;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/***
 * 描述: 数据字典缓存操作工具类
 *
 * @author wuxiang
 * @date 2020-04-14 11:04
 */
@Component("operationCommUtil")
public class OperationCommUtil {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private JedisCluster jedisCluster;

    @Resource(name = "defaultService")
    private IDefaultService defaultService;


    /**
     * 获取缓存列表：通过typeId+valId查询
     */
    public List<TOperationCommTypeVal> getDictList(String typeId,String valId) throws ServiceException {
        return getDictionariesFromDb(typeId,valId);
    }


    /**
     * 从数据库中获取字典信息
     */
    private List<TOperationCommTypeVal> getDictionariesFromDb(String typeId, String valId) throws ServiceException {
        Map<String,Object> map = new HashMap<>();
        map.put("typeId",typeId);
        map.put("valId",valId);
        List<TOperationCommTypeVal> list  = defaultService.selectList(GlobalConstants.NAME_SPACE + "getDictionariesFromDb",map);
        return list;
    }

    /**
     * 从数据库中获取字典值
     */
    private String getValueFromDb(String typeId, String valId) throws ServiceException {
        Map<String,Object> map = new HashMap<>();
        map.put("typeId",typeId);
        map.put("valId",valId);
        return defaultService.selectOne(GlobalConstants.NAME_SPACE + "getValueFromDb",map);
    }


    /**
     * 获取缓存字典值valName
     */
    public String getDictValue(String typeId, String valId, String defaultValue) {
        // 首先取缓存
        String valueName = jedisCluster.get(typeId +":" + valId);
        if (!StringUtils.isEmpty(valueName)) {
            return valueName;
        }
        // 缓存没有就从数据库获取
        valueName = getValueFromDb(typeId,valId);
        if (!StringUtils.isEmpty(valueName)) {
            jedisCluster.set(typeId+":"+valId,valueName);
        } else {
            valueName = defaultValue;
            logger.warn("缓存被穿透了，请及时检查数据库内是否有该数据字典值{}-{}",typeId,valId);
            /**预防高并发下的缓存穿透，数据库没有值也放入缓存中，并设置缓存生存时间*/
            jedisCluster.set(typeId+":"+valId,valueName);
            jedisCluster.expire(typeId+":"+valId,GlobalConstants.CACHE_EXPIRE_TIME);
        }
        return valueName;
    }

    /**
     * 新增数据字典
     */
    public int addDictonaries(OperateDictionariesDto operateDictionariesDto) throws ServiceException {
        String seqId = UUID.randomUUID().toString();
        operateDictionariesDto.setSeqId(seqId);
        return defaultService.insert(GlobalConstants.NAME_SPACE + "addDictonaries", operateDictionariesDto);
    }

    /**
     * 删除数据字典
     */
    public int delDictonaries(OperateDictionariesDto operateDictionariesDto) throws ServiceException {
        return defaultService.insert(GlobalConstants.NAME_SPACE + "delDictonaries", operateDictionariesDto);
    }

    /**
     * 更新数据字典
     */
    public int updDictonaries(OperateDictionariesDto operateDictionariesDto) throws ServiceException {
        return defaultService.update(GlobalConstants.NAME_SPACE + "updDictonaries", operateDictionariesDto);
    }

}
